﻿// Copyright (c) Microsoft Corporation
// All rights reserved

namespace Microsoft.Windows.Controls.Specialized.TextModel
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;

    /// <summary>
    /// Describes a possibly empty span of text with respect to some (unspecified) text buffer.
    /// </summary>
    public class Span
    {
        int _start, _length;

        public Span()
        {
        }

        public Span(int start, int length)
        {
            if (start < 0)
            {
                throw new ArgumentOutOfRangeException("start");
            }
            if (length < 0 || start + length == int.MaxValue)
            {
                throw new ArgumentOutOfRangeException("length");
            }

            _start = start;
            _length = length;
        }

        /// <summary>
        /// The index of the first character of the span.
        /// </summary>
        public virtual int Start
        {
            get { return _start; }
        }

        /// <summary>
        /// The index of the last character of the span. 
        /// </summary>
        public virtual int End
        {
            get { return _start + _length; }
        }

        /// <summary>
        /// The length of the span, which is always nonnegative.
        /// </summary>
        public virtual int Length
        {
            get { return _length; }
        }

        public override string ToString()
        {
            StringBuilder s = new StringBuilder("[");
            s.Append(Start);
            s.Append("..");
            s.Append(End);
            s.Append("]");
            return s.ToString();
        }
    }
}
